package com.ladybug.flow.modules.system.controller;

import com.ladybug.flow.common.annotation.LoginUser;
import com.ladybug.flow.common.cache.RedisCache;
import com.ladybug.flow.common.security.dto.LoginUserDTO;
import com.ladybug.flow.common.vo.ResponseResult;
import com.ladybug.flow.modules.system.domain.entitys.SysUser;
import com.ladybug.flow.modules.system.service.LoginServcie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
* @description: TODO
* @author long
* @date 2022/10/14 14:56
* @version 1.0
*/
@RestController
public class LoginController {
    @Autowired
    private LoginServcie loginServcie;
    @Autowired
    private RedisCache redisCache;

    /*
     * @Author long
     * @Description 登录
     * @Date 11:16 2022/10/17
     * @Param [user]
     * @return com.ladybug.lowcode.common.vo.ResponseResult
     **/
    @PostMapping("/login")
    public ResponseResult login(@RequestBody SysUser user){
        return loginServcie.login(user);
    }

    /*
     * @Author long
     * @Description 登出
     * @Date 11:16 2022/10/17
     * @Param []
     * @return com.ladybug.lowcode.common.vo.ResponseResult
     **/
    @GetMapping("/logOut")
    public ResponseResult logout(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUserDTO loginUserDTO = (LoginUserDTO) authentication.getPrincipal();
        String userid = loginUserDTO.getUser().getId();
        redisCache.deleteObject("login:"+userid);
        return new ResponseResult(200,"退出成功");
    }

    @GetMapping("/hellow")
    public String hellow(@LoginUser LoginUserDTO loginUserDTO){
        return loginUserDTO.getUsername() + "---" + loginUserDTO.getUserAccount();
    }
}
